'''
Author: your name
Date: 2021-11-19 00:01:15
LastEditTime: 2021-11-21 21:02:16
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: \cpp11d:\project\conan_repo\gtest\conanfile.py
'''
from conans import ConanFile, CMake, tools
from conans import tools
import shutil


class GenerallibConan(ConanFile):
    name = "gtest"
    version = "1.0"
    license = "GPL"
    author = "caiyuzheng"
    url = "https://github.com/google/googletest"
    description = "gtest"
    generators = "cmake_find_package"

    def source(self):
        tools.rmdir("pkgsrc")
        print("source installed to /pkgsrc")
        git = tools.Git(folder="pkgsrc", verify_ssl=False)

        git.clone("https://gitee.com/baihezhe/gtest.git")

    def package(self):
        self.copy(pattern="*.lib", src="pkgsrc\\lib\\Debug\\",
                  dst="lib", keep_path=False)
        shutil.copytree(self.build_folder + '\\pkgsrc\\googletest\\include\\gtest',
                        self.package_folder + "\\include\\gtest")
        shutil.copytree(self.build_folder + '\\pkgsrc\\googlemock\\include\\gmock',
                        self.package_folder + "\\include\\gmock")

    def build(self):
        cmake = CMake(self)
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake  -DCMAKE_MODULE_PATH=" + tools.unix_path(self.source_folder)
                                          + " -G\"MinGW Makefiles\" " + tools.unix_path(self.source_folder) + "//pkgsrc")
                tools.run_in_windows_bash(
                    self, subsystem="msys", cwd="pkgsrc", bashcmd="cmake --build .")
            if tools.get_env("BUILD_ENV") == "msvc64":
                print("msvc build")
                self.run("cmake .", cwd="pkgsrc", )
                self.run("cmake --build .", cwd="pkgsrc")

    def package_info(self):
        self.cpp_info.name = 'gtest'
        self.cpp_info.names["cmake_find_package"] = "gtest"
        self.cpp_info.names["cmake_find_package_multi"] = "gtest"

        if tools.get_env("BUILD_ENV") == "msys":
            print("msys build")
            self.cpp_info.names["cmake_find_package"] = "gtest"
            self.cpp_info.names["cmake_find_package_multi"] = "gtest"
            # Directories where libraries can be found
            self.cpp_info.libdirs = ['lib']
            self.cpp_info.libs = [
                'gtestd.a', 'gtest_maind.a', 'gmockd.a', 'gmock_maind.a']
        if tools.get_env("BUILD_ENV") == "msvc64":
            self.cpp_info.names["cmake_find_package"] = "gtest"
            self.cpp_info.names["cmake_find_package_multi"] = "gtest"
            # Directories where libraries can be found
            self.cpp_info.libs = [
                'gtestd.lib', 'gtest_maind.lib', 'gmockd.lib', 'gmock_maind.lib']
            self.cpp_info.buildirs = 'gtest'
